import Vue from 'vue'
import App from './App.vue'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import axios from 'axios'
// 引入 iconfont
import '@/assets/iconfont/iconfont.css'
// 引入 vuex
import store from './store'

Vue.use(ElementUI)
Vue.config.productionTip = false

// 配置axios
axios.defaults.baseURL = 'http://localhost:8848'
axios.defaults.headers.common['Accept'] = 'application/json, application/*+json'

//添加请求拦截器
axios.interceptors.request.use(config => {
  const token = localStorage.getItem('token')
  if (token) {
    // config.headers.Authorization = `Bearer ${token}`
    config.headers.Authorization = token
  }
  return config
})

//添加响应拦截器
axios.interceptors.response.use(
  response => response,
  error => {
    if (error.response.status === 401) {
      ElementUI.Message.error('登录状态已过期，请重新登录')
      router.push('/login')
    }
    return Promise.reject(error)
  }
)

Vue.prototype.$axios = axios

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')    